package com.yaha.controler.widgets;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yaha.controler.R;
import com.yaha.controler.utils.MyLog;
import com.yaha.controler.utils.WifiUtil;

/**
 * <功能描述>显示Wifi列表的PopWindow
 * 
 * @author Frewen.W/909091
 * @version [版本号, 2015-4-7]
 * @since [产品/模块版本]
 */
public class WifiPopWindow extends PopupWindow {
	private static final String TAG = WifiPopWindow.class.getSimpleName();
	private Context mContext;
	private View conentView;
	private ListView mWifiListView;
	private WifiUtil mWifiUtil;
	private WifiListAdapter mWifiListAdapter;
	private List<ScanResult> mWifiList = new ArrayList<ScanResult>();;

	public WifiPopWindow(final Activity context) {
		this.mContext = context;
		initWifiList();
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.wifi_popup_dialog, null);

		mWifiListView = (ListView) conentView.findViewById(R.id.wifi_list);
		mWifiListAdapter = new WifiListAdapter(context);
		mWifiListAdapter.setData(mWifiList);
		mWifiListView.setAdapter(mWifiListAdapter);

		mWifiListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				WifiConfiguration wifiConfiguration = mWifiUtil.IsExsits(mWifiList.get(position).SSID);
				if (null != wifiConfiguration) {
					Log.i(TAG, "配置过，");
					mWifiUtil.getmWifiManager().enableNetwork(wifiConfiguration.networkId, true);
					// 点击之后触发
					mWifiListAdapter.notifyDataSetChanged();
				} else {
					Log.i(TAG, "未配置过，");
					// TODO 未配置过的话，弹出输入密码框，进入密码输入继续宁配置
				}
			}
		});

		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 得到PopuWindow的高和宽
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w / 2 + 50);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(h / 2 + 50);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		// ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_popupwindow));
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);
	}

	/**
	 * <功能描述>初始化Wifi列表
	 */
	private void initWifiList() {
		mWifiUtil = new WifiUtil(mContext);
		mWifiUtil.openWifi();
		mWifiUtil.startScan();
		mWifiList = mWifiUtil.getmWifiList();
	}

	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
		} else {
			this.dismiss();
		}
	}

	/**
	 * <功能描述>WifiList适配器类
	 * 
	 * @author Frewen.W/909091
	 * @version [版本号, 2015-4-8]
	 * @since [产品/模块版本]
	 */
	public class WifiListAdapter extends BaseAdapter {
		private Context mContext;
		private List<ScanResult> mResults;

		public WifiListAdapter(Activity context) {
			this.mContext = context;
		}

		public void setData(List<ScanResult> results) {
			this.mResults = results;

		}

		@Override
		public int getCount() {
			return mResults.size();
		}

		@Override
		public Object getItem(int position) {
			return mResults.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder mViewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.wifi_popup_dialog_item, null);
				mViewHolder = new ViewHolder();
				mViewHolder.mWifiName = (TextView) convertView.findViewById(R.id.wifi_item_text);
				mViewHolder.mWifiDegree = (ImageView) convertView.findViewById(R.id.wifi_item_degree);
				mViewHolder.mWifiConnected = (TextView) convertView.findViewById(R.id.wifi_item_connected);
				convertView.setTag(mViewHolder);
			} else {
				mViewHolder = (ViewHolder) convertView.getTag();
			}
			mViewHolder.mWifiName.setText(mResults.get(position).SSID);
			String connectedSSID = "\"" + mResults.get(position).SSID + "\"";

			if (connectedSSID.equals(mWifiUtil.getmWifiInfo().getSSID())) {
				mViewHolder.mWifiConnected.setText("已连接");
			} else {
				mViewHolder.mWifiConnected.setText("");
			}

			return convertView;
		}

		class ViewHolder {
			TextView mWifiName;
			ImageView mWifiDegree;
			TextView mWifiConnected;
		}

	}
}
